﻿// -----------------------------------------------------------------------
// <copyright file="PhuongXaData.cs" company="VDC" author="Nguyễn Quyết">
// Class PhuongXaData
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITY;
    
    /// <summary>
    ///     Class PhuongXaData.cs
    /// </summary>
    public class PhuongXaData
    {
        /// <summary>
        /// Gets the phuong xa by id.
        /// </summary>
        /// <param name="idPhuongXa">The id phuong xa.</param>
        /// <returns>return phuongXa</returns>
        public PhuongXa GetPhuongXaById(int idPhuongXa)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();

            LogHelper.Instance.WriteError(Constants.Begin + methodName);
            try
            {
                PhuongXa phuongXa = null;

                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePhuongXaGetById, idPhuongXa.ToString());
                while (reader.Read())
                {
                    phuongXa = new PhuongXa();
                    if (!reader.IsDBNull(0))
                    {
                        phuongXa.IdPhuongXa = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        phuongXa.IdQuanHuyen = reader.GetInt32(1);
                    }

                    if (reader.GetString(2) != null)
                    {
                        phuongXa.MaPhuongXa = reader.GetString(2);
                    }

                    if (reader.GetString(3) != null)
                    {
                        phuongXa.TenPhuongXa = reader.GetString(3);
                    }
                }

                reader.Close();
                reader.Dispose();

                LogHelper.Instance.WriteError(Constants.End + methodName);

                return phuongXa;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// Gets the phuong xa by id quan huyen.
        /// </summary>
        /// <param name="idQuanHuyen">The id quan huyen.</param>
        /// <returns>return phuongXa </returns>
        public PhuongXa GetPhuongXaByIdQuanHuyen(int idQuanHuyen)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();

            LogHelper.Instance.WriteError(Constants.Begin + methodName);
            try
            {
                PhuongXa phuongXa = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePhuongXaGet, idQuanHuyen.ToString());
                while (reader.Read())
                {
                    phuongXa = new PhuongXa();
                    if (!reader.IsDBNull(0))
                    {
                        phuongXa.IdPhuongXa = reader.GetInt32(0);
                    }

                    if (reader.IsDBNull(1))
                    {
                        phuongXa.IdQuanHuyen = reader.GetInt32(1);
                    }

                    if (reader.GetString(2) != null)
                    {
                        phuongXa.MaPhuongXa = reader.GetString(2);
                    }

                    if (reader.GetString(3) != null)
                    {
                        phuongXa.TenPhuongXa = reader.GetString(3);
                    }

                    if (reader.GetString(4) != null)
                    {
                        phuongXa.TenQuanHuyen = reader.GetString(4);
                    }
                }

                reader.Close();
                reader.Dispose();

                LogHelper.Instance.WriteError(Constants.End + methodName);
                return phuongXa;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// Gets the phuong xa.
        /// </summary>
        /// <returns>return List</returns>
        public IList<PhuongXa> GetPhuongXa()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();
            LogHelper.Instance.WriteError(Constants.Begin + methodName);
            try
            {
                IList<PhuongXa> listPhuongXa = new List<PhuongXa>();
                PhuongXa phuongXa;
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePhuongXaGet);
                while (reader.Read())
                {
                    phuongXa = new PhuongXa();
                    if (!reader.IsDBNull(0))
                    {
                        phuongXa.IdPhuongXa = reader.GetInt32(0);
                    }

                    if (reader.IsDBNull(1))
                    {
                        phuongXa.IdQuanHuyen = reader.GetInt32(1);
                    }

                    if (reader.GetString(2) != null)
                    {
                        phuongXa.MaPhuongXa = reader.GetString(2);
                    }

                    if (reader.GetString(3) != null)
                    {
                        phuongXa.TenPhuongXa = reader.GetString(3);
                    }

                    if (reader.GetString(4) != null)
                    {
                        phuongXa.TenQuanHuyen = reader.GetString(4);
                    }

                    listPhuongXa.Add(phuongXa);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteError(Constants.End + methodName);
                return listPhuongXa;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Inserts the specified phuong xa.
        /// </summary>
        /// <param name="phuongXa">The phuong xa.</param>
        /// <returns>return int</returns>
        public int Insert(PhuongXa phuongXa)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();

            LogHelper.Instance.WriteError(Constants.Begin + methodName);
            try
            {
                var sqlParams = new SqlParameter[]
                {
                    new SqlParameter(Constants.SqlParamerterName.IdQuanHuyen, phuongXa.IdQuanHuyen),
                    new SqlParameter(Constants.SqlParamerterName.MaPhuongXa, phuongXa.MaPhuongXa),
                    new SqlParameter(Constants.SqlParamerterName.TenPhuongXa, phuongXa.TenPhuongXa),
                };
                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePhuongXaInsert, sqlParams));
                LogHelper.Instance.WriteError(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Updates the specified phuong xa.
        /// </summary>
        /// <param name="phuongXa">The phuong xa.</param>
        public void Update(PhuongXa phuongXa)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();

            LogHelper.Instance.WriteError(Constants.Begin + methodName);
            try
            {
                var sqlParams = new SqlParameter[]
                    {
                        new SqlParameter(Constants.SqlParamerterName.IdPhuongXa, phuongXa.IdPhuongXa),
                        new SqlParameter(Constants.SqlParamerterName.IdQuanHuyen, phuongXa.IdQuanHuyen),
                        new SqlParameter(Constants.SqlParamerterName.MaPhuongXa, phuongXa.MaPhuongXa),
                        new SqlParameter(Constants.SqlParamerterName.TenPhuongXa, phuongXa.TenPhuongXa)
                    };
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePhuongXaUpdate, sqlParams);
                LogHelper.Instance.WriteError(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Deletes the specified id phuong xa.
        /// </summary>
        /// <param name="idPhuongXa">The id phuong xa.</param>
        public void Delete(int idPhuongXa)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();

            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParams = new SqlParameter[] { new SqlParameter(Constants.SqlParamerterName.IdPhuongXa, idPhuongXa) };
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePhuongXaDelete, sqlParams);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Tims the kiem phuong xa.
        /// </summary>
        /// <param name="ma_phuong_xa">The ma_phuong_xa.</param>
        /// <param name="ten_phuong_xa">The ten_phuong_xa.</param>
        /// <param name="id_tinh_thanh">The id_tinh_thanh.</param>
        /// <param name="id_quan_huyen">The id_quan_huyen.</param>
        /// <returns>IList PhuongXa</returns>
        public IList<PhuongXa> TimKiemPhuongXa(string ma_phuong_xa, string ten_phuong_xa, int id_tinh_thanh, int id_quan_huyen)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();
            LogHelper.Instance.WriteError(Constants.Begin + methodName);
            try
            {
                IList<PhuongXa> listPhuongXa = new List<PhuongXa>();
                PhuongXa phuongXa;
                string[] param = new string[4];
                param[0] = ma_phuong_xa.ToString();
                param[1] = ten_phuong_xa.ToString();
                param[2] = id_quan_huyen.ToString();
                param[3] = id_tinh_thanh.ToString();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePhuongXaSearch, param);
                while (reader.Read())
                {
                    phuongXa = new PhuongXa();
                    if (!reader.IsDBNull(0))
                    {
                        phuongXa.IdPhuongXa = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        phuongXa.MaPhuongXa = reader.GetString(1);
                    }

                    if (reader.GetString(2) != null)
                    {
                        phuongXa.TenPhuongXa = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        phuongXa.TenQuanHuyen = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        phuongXa.TenTinhThanh = reader.GetString(4);
                    }

                    listPhuongXa.Add(phuongXa);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteError(Constants.End + methodName);
                return listPhuongXa;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Checks the ma phuong xa.
        /// </summary>
        /// <param name="ma_phuong_xa">The maphuongxa.</param>
        /// <returns>var result</returns>
        public int CheckMaPhuongXa(string ma_phuong_xa)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
            {
                new SqlParameter(Constants.SqlParamerterName.MaPhuongXa, ma_phuong_xa) 
            };
                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePhuongXaCheckMa, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }
    }
}
